package com.py.credit.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.CreditStandardCommitment;
import com.py.credit.entity.SysUser;
import com.py.credit.service.CreditStandardCommitmentService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

@RestController
@RequestMapping("/creditStandardCommitment")
public class CreditStandardCommitmentController {
	@Autowired
	private CreditStandardCommitmentService creditStandardCommitmentService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Value("${imageurl}")
	private String imageurl;

	@Value("${imagepath}")
	private String imagepath;

	@RequestMapping("/upload.do")
	public RestServiceResult<Object> pic(@RequestParam("file") MultipartFile file) throws IOException {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		InputStream input = file.getInputStream();
		String originalFilename = file.getOriginalFilename();
		String subfix = originalFilename.substring(originalFilename.lastIndexOf("."));
		String picName = System.currentTimeMillis() + subfix;
		String path = imagepath + File.separator + picName;
		String picurl = imageurl + File.separator + picName;
		FileOutputStream fout = new FileOutputStream(path);
		byte[] b = new byte[1024];
		int length = 0;
		while ((length = input.read(b)) > 0) {
			fout.write(b, 0, length);
		}
		input.close();
		fout.close();
		Map<String, String> resultPath = new HashMap<String, String>();
		// resultPath.put("picName", picName);
		resultPath.put("picurl", picurl);
		result.setData(resultPath);
		return result;
	}
	// 增加

	@RequestMapping("/insert.do")
	public RestServiceResult<Object> insert(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "content", defaultValue = "", required = false) String content,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "pic", defaultValue = "", required = false) String pic,
			@RequestParam(value = "summary", defaultValue = "", required = false) String summary,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, HttpServletRequest request)
			throws IOException {
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser usrInfo = new SysUser();
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			usrInfo = user.getData();
		}
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setContent(content);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setPic(pic);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setUserId(String.valueOf(usrInfo.getId()));
		creditStandardCommitment.setSummary(summary);
		creditStandardCommitment.setSfzl(sfzl);

		try {
			creditStandardCommitmentService.insert(creditStandardCommitment);
			result.setMsg("添加成功");
		} catch (Exception e) {
			result.setMsg("添加失败");
		}
		return result;

	}

	@RequestMapping("/selectForPage.json")
	public RestServiceResult<List<CreditStandardCommitment>> selectForPage(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, Page page) {
		RestServiceResult<List<CreditStandardCommitment>> result = new RestServiceResult<List<CreditStandardCommitment>>();
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setSfzl(sfzl);
		try {
			List<CreditStandardCommitment> tableList = creditStandardCommitmentService
					.selectForPage(creditStandardCommitment, page);
			result.setData(tableList);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	@RequestMapping("/selectByStateForPage.json")
	public RestServiceResult<List<CreditStandardCommitment>> selectByStateForPage(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, Page page) {
		RestServiceResult<List<CreditStandardCommitment>> result = new RestServiceResult<List<CreditStandardCommitment>>();
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setSfzl(sfzl);
		try {
			List<CreditStandardCommitment> tableList = creditStandardCommitmentService
					.selectByStateForPage(creditStandardCommitment, page);
			result.setData(tableList);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	@RequestMapping("/delListById.do")
	public RestServiceResult<Object> delListById(
			@RequestParam(value = "ids", defaultValue = "", required = false) String strId) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		List<String> idList = new ArrayList<String>();
		idList = java.util.Arrays.asList(strId.split(","));
		try {
			creditStandardCommitmentService.delListById(idList);
			result.setMsg("删除成功");
		} catch (Exception e) {
			result.setMsg("删除失败");
		}
		return result;
	}

	@RequestMapping("/updateById.do")
	public RestServiceResult<Object> updateById(
			@RequestParam(value = "id", defaultValue = "", required = false) String id,
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "content", defaultValue = "", required = false) String content,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "pic", defaultValue = "", required = false) String pic,
			@RequestParam(value = "summary", defaultValue = "", required = false) String summary,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, HttpServletRequest request)
			throws IOException {
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		creditStandardCommitment.setId(Integer.valueOf(id));
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setContent(content);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setPic(pic);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setSummary(summary);
		creditStandardCommitment.setSfzl(sfzl);
		try {
			creditStandardCommitmentService.updateById(creditStandardCommitment);
			result.setMsg("修改成功");
		} catch (Exception e) {
			result.setMsg("修改失败");
		}
		return result;
	}

	@RequestMapping("/selectById.json")
	public RestServiceResult<CreditStandardCommitment> selectById(
			@RequestParam(value = "id", defaultValue = "", required = false) String id) {
		RestServiceResult<CreditStandardCommitment> result = new RestServiceResult<CreditStandardCommitment>();
		Integer newId = 0;
		if (StringUtils.isNotEmpty(id)) {
			newId = Integer.valueOf(id);
		}
		try {
			CreditStandardCommitment creditStandardCommitment = creditStandardCommitmentService.selectById(newId);
			result.setData(creditStandardCommitment);
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	@RequestMapping("/selectByType.json")
	public RestServiceResult<CreditStandardCommitment> selectByType() {
		RestServiceResult<CreditStandardCommitment> result = new RestServiceResult<CreditStandardCommitment>();

		try {
			CreditStandardCommitment creditStandardCommitment = creditStandardCommitmentService.selectByType();
			result.setData(creditStandardCommitment);
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	@RequestMapping("/selectByMlType.json")
	public RestServiceResult<CreditStandardCommitment> selectByMlType() {
		RestServiceResult<CreditStandardCommitment> result = new RestServiceResult<CreditStandardCommitment>();

		try {
			CreditStandardCommitment creditStandardCommitment = creditStandardCommitmentService.selectByMlType();
			result.setData(creditStandardCommitment);
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}
	@IgnoreAuthority
	@RequestMapping("/selectCriterionForPage.json")
	public RestServiceResult<List<CreditStandardCommitment>> selectCriterionForPage(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, Page page) {
		RestServiceResult<List<CreditStandardCommitment>> result = new RestServiceResult<List<CreditStandardCommitment>>();
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setSfzl(sfzl);

		try {
			List<CreditStandardCommitment> tableList = creditStandardCommitmentService
					.selectCriterionForPage(creditStandardCommitment, page);
			result.setData(tableList);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	@RequestMapping("/selectCriterionByStateForPage.json")
	public RestServiceResult<List<CreditStandardCommitment>> selectCriterionByStateForPage(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, Page page) {
		RestServiceResult<List<CreditStandardCommitment>> result = new RestServiceResult<List<CreditStandardCommitment>>();
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setSfzl(sfzl);

		try {
			List<CreditStandardCommitment> tableList = creditStandardCommitmentService
					.selectCriterionByStateForPage(creditStandardCommitment, page);
			result.setData(tableList);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	@RequestMapping("/updateStateById.do")
	public RestServiceResult<Object> updateStateById(
			@RequestParam(value = "id", defaultValue = "", required = false) String id,
			@RequestParam(value = "state", defaultValue = "", required = false) String state,
			HttpServletRequest request) throws IOException {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			creditStandardCommitmentService.updateStateById((Integer.valueOf(id)), state);
			result.setMsg("修改成功");
		} catch (Exception e) {
			result.setMsg("修改失败");
		}
		return result;
	}

	@RequestMapping("/selectContentsForPage.json")
	public RestServiceResult<List<CreditStandardCommitment>> selectContentsForPage(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, Page page) {
		RestServiceResult<List<CreditStandardCommitment>> result = new RestServiceResult<List<CreditStandardCommitment>>();
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setSfzl(sfzl);

		try {
			List<CreditStandardCommitment> tableList = creditStandardCommitmentService
					.selectContentsForPage(creditStandardCommitment, page);
			result.setData(tableList);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	@RequestMapping("/selectContentsByStateForPage.json")
	public RestServiceResult<List<CreditStandardCommitment>> selectContentsByStateForPage(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "type", defaultValue = "", required = false) String type,
			@RequestParam(value = "suorce", defaultValue = "", required = false) String suorce,
			@RequestParam(value = "deptId", defaultValue = "", required = false) String deptId,
			@RequestParam(value = "sfzl", defaultValue = "", required = false) String sfzl, Page page) {
		RestServiceResult<List<CreditStandardCommitment>> result = new RestServiceResult<List<CreditStandardCommitment>>();
		CreditStandardCommitment creditStandardCommitment = new CreditStandardCommitment();
		creditStandardCommitment.setTitle(title);
		creditStandardCommitment.setType(type);
		creditStandardCommitment.setSuorce(suorce);
		creditStandardCommitment.setDeptId(deptId);
		creditStandardCommitment.setSfzl(sfzl);

		try {
			List<CreditStandardCommitment> tableList = creditStandardCommitmentService
					.selectContentsByStateForPage(creditStandardCommitment, page);
			result.setData(tableList);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}

	// 页面查询单条
	@RequestMapping("/selectOneById.json")
	public RestServiceResult<CreditStandardCommitment> selectOneById(
			@RequestParam(value = "id", defaultValue = "", required = false) String id) {
		RestServiceResult<CreditStandardCommitment> result = new RestServiceResult<CreditStandardCommitment>();

		try {
			CreditStandardCommitment creditStandardCommitment = creditStandardCommitmentService.selectOneById(id);
			result.setData(creditStandardCommitment);
			creditStandardCommitmentService.updateOneById(id);
		} catch (Exception e) {
			result.setMsg("查询失败");
			;
		}
		return result;
	}
}
